package com.juyu.miliao.api.controller.whitepacket;

import com.juyu.miliao.api.domain.dto.WhitePacketDynamicCommentInfoDTO;
import com.juyu.miliao.api.service.whitepacket.WhitePacketDynamicCommentInfoService;
import com.juyu.miliao.api.util.PageInfoUtil;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author xuanqicheng
 * @create 2019-21-12:51
 */
@RestController
@RequestMapping("/packetComment")
public class WhitePacketDynamicCommentInfoController {

    @Autowired
    private WhitePacketDynamicCommentInfoService dynamicCommentInfoService;
    /**
     * 添加评论
     */
    @PostMapping("/")
    public void addDynamicComment(Authentication token, @Validated WhitePacketDynamicCommentInfoDTO dynamicCommentInfoDTO,
                                  BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw  new ConsumeRuntimeException(bindingResult.getFieldError().getDefaultMessage());
        }

        Subject subject = (Subject) token.getPrincipal();

        dynamicCommentInfoDTO.setFromUserId(subject.getId());

        dynamicCommentInfoService.addDynamicComment(dynamicCommentInfoDTO);

    }


    /**
     * 动态评论信息
     * @param dynamicId
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/{dynamicId}/{page}-{pageSize}")
    public Object getDynamicInfoList(@PathVariable("dynamicId") long dynamicId,@PathVariable("page") int page,
                                     @PathVariable("pageSize") int pageSize) {

        PageInfoUtil pageInfo = new PageInfoUtil(page, pageSize).invoke();

        page = pageInfo.getPage();

        pageSize = pageInfo.getPageSize();

        Object obj = dynamicCommentInfoService.getDynamicCommentPageByDynamicId(dynamicId,page,pageSize);
        return obj;
    }


    /**
     * 获取评论与回复的详细信息
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public Object getDetailId(@PathVariable("id") long id){
        return dynamicCommentInfoService.getDetailInfo(id);
    }

}
